package com.tjtt.tdm.form.dao.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.tjtt.tdm.flow.bean.Flowtype;
import com.tjtt.tdm.form.dao.FlowtypeDao;


public class FlowtypeDaoImpl extends HibernateDaoSupport implements FlowtypeDao{
	private static final Log log = LogFactory.getLog(FlowtypeDao.class);
	// property constants
	public static final String NAME = "name";

	protected void initDao() {
		// do nothing
	}

	public String save(Flowtype transientInstance) {
		log.debug("saving Flowtype instance");
		try {
			String id = "";
			id = getHibernateTemplate().save(transientInstance).toString();
			log.debug("save successful");
			return id;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			return "";
		}
	}
	
	public String saveobject(Object transientInstance) {
		log.debug("saving Flowtype instance");
		try {
			String id = "";
			id = getHibernateTemplate().save(transientInstance).toString();
			log.debug("save successful");
			return id;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			return "";
		}
	}

	public void delete(Flowtype persistentInstance) {
		log.debug("deleting Flowtype instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Flowtype findById(java.lang.Long id) {
		log.debug("getting Flowtype instance with id: " + id);
		try {
			Flowtype instance = (Flowtype) getHibernateTemplate().get(
					"com.tjtt.tdm.model.Flowtype", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Flowtype instance) {
		log.debug("finding Flowtype instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Flowtype instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Flowtype as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}
	
	public List findAllByWhere(String where) {
		log.debug("finding all Flowtype instances");
		try {
			String queryString = "from Flowtype where "+where;
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all Flowtype instances");
		try {
			String queryString = "from Flowtype";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAllBySql(String sql,int start,int rows)
	{
		log.debug("finding all Flowtype instances");
		try {
			Query query = getSession().createSQLQuery(sql);
			query.setFirstResult(start);
			query.setMaxResults(rows);
			List list = query.list();
			return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List findAllBySql(String sql)
	{
		log.debug("finding all Flowtype instances");
		try {
			Query query = getSession().createSQLQuery(sql);
			List list = query.list();
			return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public void executeBySql(String sql)
	{
		log.debug("finding all Flowtype instances");
		try {
			getSession().createSQLQuery(sql).executeUpdate();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Flowtype merge(Flowtype detachedInstance) {
		log.debug("merging Flowtype instance");
		try {
			Flowtype result = (Flowtype) getHibernateTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Flowtype instance) {
		log.debug("attaching dirty Flowtype instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Flowtype instance) {
		log.debug("attaching clean Flowtype instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	public Long getMaxId()
	{
		log.debug("获得最大的ID值");
		try {
			Query query = getSession().createSQLQuery("select id from flow_flowtype order by id desc");
			List list = query.list();
			if (list.size()==0)
			{
				return Long.parseLong("0")+1;
			}
			else
			{
				String maxID = list.get(0).toString();
				return Long.parseLong(maxID)+1;
			}
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public void delFlowtypeById(String id)
	{
		try {
			getSession().createSQLQuery("delete from flow_flowtype where id="+id).executeUpdate();
		} catch (RuntimeException re) {
			log.error("del by id", re);
			throw re;
		}
	}
	
	public void update(Flowtype instance) {
		log.debug("update dirty Flowtype instance");
		try {
			getHibernateTemplate().update(instance);
			log.debug("update successful");
		} catch (RuntimeException re) {
			log.error("update failed", re);
			throw re;
		}
	}
	

	public static FlowtypeDao getFromApplicationContext(ApplicationContext ctx) {
		return (FlowtypeDao) ctx.getBean("FlowtypeDao");
	}
}
